function [Q, pvalue] = zFC_LjungBox(series,S)

% Computes Q test for white noise
% checked on stata

T =  length(series);

step1 = zeros(1,S);

for s=1:S
    step1(s) = zFC_acf(series,s);
end

step1 = step1.^2;

step3 = T*ones(1,S);
step4 = 1:1:S;
step3 = step3 - step4;

step5 = step1./step3;

Q = T*(T+2)*sum(step5);

pvalue = 1-chi2cdf(Q,S);

end
